#include <iostream>
#include <chrono>
#include "connectPool.h"

using namespace std;

void op1()
{
    const char *host = "localhost";
    const char *user = "root";
    const char *passwd = "1";

    for(int idx = 0; idx < 5000; idx++)
    {
        /* 连接数据库 */
        mysqlDataBase conn(host, user, passwd);
        conn.connectDB("musicDB");

        const char *sql = "insert into userInfo(username, age) value('zhangsan', 20);";

        /* 执行sql语句 */
        conn.executeSql(sql);

        /* 关闭数据库 */
        conn.close();

    }

}

void op2(ConnectPool * pool)
{
    for(int idx = 0; idx < 5000; idx++)
    {
        shared_ptr<mysqlDataBase> conn = pool->getConnection();
        
        const char *sql = "insert into userInfo(username, age) value('zhangsan', 20);";
        
        conn->executeSql(sql);
    }
}

void test()
{   

#if 0
    chrono::_V2::steady_clock::time_point startTime = chrono::steady_clock::now();
    /* 不使用连接池 */
    op1();
    chrono::_V2::steady_clock::time_point endTime = chrono::steady_clock::now();

    auto interval = endTime - startTime;
    cout << "interval:" << interval.count() << endl;

#else
    chrono::_V2::steady_clock::time_point startTime = chrono::steady_clock::now();
    /* 使用连接池 */
    ConnectPool * pool = ConnectPool::getConnectPool();
    op2(pool);
    chrono::_V2::steady_clock::time_point endTime = chrono::steady_clock::now();
    auto interval = endTime - startTime;
    cout << "interval:" << interval.count() << endl;
#endif
}

int main()
{
    test();
    return 0;
}